package com.yunji.web.system;

import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.model.domain.SysPostDO;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.system.context.SystemContext;
import com.yunji.system.service.ISysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "岗位信息操作处理")
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {
    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @ApiOperation("获取岗位列表")
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public Response<List<SysPostDO>> list(SysPostDO post) {
        List<SysPostDO> list = postService.selectPostList(post);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出岗位信息")
    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:post:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPostDO post) {
        List<SysPostDO> list = postService.selectPostList(post);
        ExcelUtil<SysPostDO> util = new ExcelUtil<>(SysPostDO.class);
        util.exportExcel(response, list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @ApiOperation("根据岗位编号获取详细信息")
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public Response<SysPostDO> getInfo(@PathVariable Long postId) {
        return ResponseHelper.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @ApiOperation("新增岗位")
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Response<Long> add(@Validated @RequestBody SysPostDO post) {
        if (!postService.checkPostNameUnique(post)) {
            return ResponseHelper.failure("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return ResponseHelper.failure("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(SystemContext.getUsername());
        Assert.isTrue(postService.insertPost(post) > 0, "新增失败");
        return ResponseHelper.success(post.getPostId());
    }

    /**
     * 修改岗位
     */
    @ApiOperation("修改岗位")
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response<String> edit(@Validated @RequestBody SysPostDO post) {
        if (!postService.checkPostNameUnique(post)) {
            return ResponseHelper.failure("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return ResponseHelper.failure("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(SystemContext.getUsername());
        Assert.isTrue(postService.updatePost(post) > 0, "修改失败");
        return ResponseHelper.success();
    }

    /**
     * 删除岗位
     */
    @ApiOperation("删除岗位")
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public Response<String> remove(@PathVariable Long[] postIds) {
        Assert.isTrue(postService.deletePostByIds(postIds) > 0, "删除失败");
        return ResponseHelper.success();
    }

    /**
     * 获取岗位选择框列表
     */
    @ApiOperation("获取岗位选择框列表")
    @GetMapping("/optionselect")
    public Response<List<SysPostDO>> optionselect() {
        List<SysPostDO> posts = postService.selectPostAll();
        return ResponseHelper.success(posts);
    }
}
